// body-data-list.js
var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({
  data: {
    config: app.config,
    globalData: app.globalData,
    screenHeight: 0,
    bodyDatas: [],
    needCb: 0,
    first: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var screenHeight = wx.getSystemInfoSync().screenHeight;

    if (options.needCb) {
      this.setData({
        screenHeight: screenHeight,
        needCb: options.needCb
      });
    } else {
      this.setData({
        screenHeight: screenHeight
      });
    }
  },

  onShow: function () {
    console.log('onShow')
    this.loadData();
  },

  loadData: function(e) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });
    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      iFDU.api.doGet(
        'bodyshape/getBodyShapeList',
        {
          userId: globalData.userId,
          rnd: Math.random()
        },
        function (data) {
          if (that.data.first) {
            if (!data || data.length < 1) {
              that.addNewBodyData();
              that.setData({
                first: false,
              });
            } else {
              that.setData({
                first: false,
                bodyDatas: data
              });
            }
          } else {
            if (!data || data.length < 1) {
              that.setData({
                bodyDatas: data
              });
            } else {
              that.setData({
                bodyDatas: data
              });
            }
          }
          wx.hideLoading();
        }
      );
    });
  },

  addNewBodyData: function (e) {
    var that = this;
    if (!that.data.needCb || that.data.needCb == 0) {
      wx.navigateTo({
        url: '../body-data/body-data?needCb=' + that.data.needCb,
      })
    } else {
      wx.redirectTo({
        url: '../body-data/body-data?needCb=' + that.data.needCb,
      })
    }
  },

  setDefaultBodyShape: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    iFDU.api.doGet(
      'bodyshape/setDefaultBodyShape',
      {
        userId: that.data.globalData.userId,
        rnd: Math.random(),
        id: id
      },
      function (data) {
        var bodyDatas = that.data.bodyDatas;
        var len = bodyDatas.length;
        for (var i = 0; i < len; i++) {
          if (id == bodyDatas[i].id) {
            bodyDatas[i].is_default = 1;
          } else {
            bodyDatas[i].is_default = 0;
          }
        }

        that.setData({
          bodyDatas: bodyDatas
        });
      }
    );
  },

  editBodyShapeData: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var bodyDatas = that.data.bodyDatas;
    var len = bodyDatas.length;
    var bodyData;
    for (var i = 0; i < len; i++) {
      if (id == bodyDatas[i].id) {
        bodyData = bodyDatas[i];
        break;
      }
    }
    if (bodyData) {
      var bodyShape = {};
      bodyShape['0'] = bodyData.bust_length;
      bodyShape['1'] = bodyData.waistline;
      bodyShape['2'] = bodyData.sleeve_length;
      bodyShape['3'] = bodyData.height;
      bodyShape['4'] = bodyData.weight;
      bodyShape['5'] = bodyData.collar_length;
      bodyShape['6'] = bodyData.shoulder_length;
      wx.navigateTo({
        url: '../body-data/body-data?id=' + id + '&bodyShape=' + JSON.stringify(bodyShape) + '&wholeBodyPhoto=' + bodyData.whole_body_photo + '&remark=' + bodyData.remark,
      })
    }
  },

  deleteBodyShapeData: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除身型数据',
      content: '将此组数据从列表中删除吗？',
      confirmText: "删除",
      cancelText: "保留",
      success: function (res) {
        if (res.confirm) {
          iFDU.api.doGet(
            'bodyshape/deleteBodyShape',
            {
              userId: that.data.globalData.userId,
              rnd: Math.random(),
              id: id
            },
            function (data) {
              var bodyDatas = that.data.bodyDatas;
              var len = bodyDatas.length;
              for (var i = 0; i < len; i++) {
                if (id == bodyDatas[i].id) {
                  bodyDatas.splice(i, 1);
                  break;
                }
              }

              that.setData({
                bodyDatas: bodyDatas
              });
            }
          );
        }
      }
    });
  },

  selectBodyData: function (e) {
    var that = this;
    if (!that.data.needCb || that.data.needCb == 0) {
      return;
    }
    var id = e.currentTarget.dataset.id;
    iFDU.api.doGet(
      'bodyshape/getBodyShapeById',
      {
        userId: that.data.globalData.userId,
        id: id,
        rnd: Math.random()
      },
      function (data) {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          bodyShapeId: id,
          bodyShape: data.bodyShape
        })

        wx.navigateBack({
        })
      }
    )
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '你的衬衫，应该是定制的！',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})